<template>
  <div>
    <insert-commodity-entity
      @postInsert="queryByCondition"
    ></insert-commodity-entity>
    <el-form
      ref="searchForm"
      class="searchForm"
      :model="searchForm"
      label-width="50"
    >
      <el-row :gutter="12">
        <!--        合计24份  -->
        <el-col :span="6">
          <el-form-item prop="queryCondition.commodityCode" label="编号">
            <el-input v-model.trim="searchForm.queryCondition.commodityCode">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item
            prop="queryCondition.commodityDescrSimilar"
            label="品名"
          >
            <el-input
              v-model.trim="searchForm.queryCondition.commodityDescrSimilar"
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item prop="queryCondition.priceMin" label="最小值">
            <el-input v-model="searchForm.queryCondition.priceMin"> </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item prop="queryCondition.priceMax" label="最大值">
            <el-input v-model="searchForm.queryCondition.priceMax"> </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="12">
        <el-col :span="12">
          <span>todo:商品所属类别</span>
        </el-col>
      </el-row>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-edit"
          @click="queryByCondition(1)"
          >查询</el-button
        >
        <el-button
          type="warning"
          icon="el-icon-remove"
          @click="() => $refs.searchForm.resetFields()"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table :data="results" class="resultsTable" stripe>
      <!--
               {
      "commodityId": 2,
      "commodityCode": "CC22034305000164",
      "commodityDescr": "蒙牛利乐包纯牛奶",
      "retailPrice": "2.90",
      "chargeUnit": "盒",
      "barCode": "11112233",
      "specification": "250ml",
      "producingArea": "内蒙古第三旗",
      "qualityLevel": 1,
      "typeFk": 6,
      "isDeleted": false
    },
              -->
      <el-table-column
        prop="commodityId"
        label="ID"
        :width="50"
      ></el-table-column>
      <el-table-column
        prop="commodityCode"
        label="Code"
        :width="160"
      ></el-table-column>
      <el-table-column
        prop="commodityDescr"
        label="品名"
        :width="170"
      ></el-table-column>
      <el-table-column prop="retailPrice" label="零售价"></el-table-column>
      <el-table-column prop="chargeUnit" label="计价单位"></el-table-column>
      <el-table-column
        prop="producingArea"
        label="产地"
        :width="120"
      ></el-table-column>
      <!--      <el-table-column prop="barCode" label="条形码"></el-table-column>-->
      <el-table-column prop="specification" label="规格"></el-table-column>

      <el-table-column label="质量等级" :formatter="qualityLevelFormat">
      </el-table-column>
      <el-table-column label="类别" :width="120" :formatter="typeFkFormat">
      </el-table-column>
      <el-table-column width="180px" fixed="left">
        <template slot-scope="props">
          <el-button
            class="el-button--mini"
            type="warning"
            icon="el-icon-edit"
            @click="openById(props.row)"
            >编辑</el-button
          >
          <el-button
            class="el-button--mini"
            type="danger"
            icon="el-icon-edit"
            @click="deleteById(props.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :total="total"
      :page-size="searchForm.pageSize"
      :current-page="searchForm.pageNum"
      @current-change="queryByCondition"
      layout="total, prev, pager, next, jumper"
    ></el-pagination>
  </div>
</template>

<script>
// import New from './components/New.vue';
import { commodityModuleApi } from "@/api/apis";
import InsertCommodityEntity from "@/components/InsertCommodityEntity";
export default {
  name: "GoodsData",
  components: {
    InsertCommodityEntity,
    // New,Old,
  },
  props: {
    sampleP: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      sampleD: "value",

      searchForm: {
        pageNum: 1,
        pageSize: 8,
        queryCondition: {
          commodityCode: null,
          commodityDescrSimilar: null,
          priceMax: null,
          priceMin: null,
          types: [],
        },
      },
      results: [],
      total: 0,
    };
  },
  computed: {
    // sampleC() {
    //     return this.sampleD+'--';
    // }
  },
  created() {},
  mounted() {
    this.queryByCondition(1);
  },
  methods: {
    sampleM() {},
    queryByCondition(pageNum) {
      if (pageNum) this.searchForm.pageNum = pageNum;
      // this.$http({
      //   method: "post",
      //   url: "/query/commodityEnity",
      //   data: this.searchForm,
      // })
      commodityModuleApi.cmEntityQueryApi(this.searchForm).then(
        (res) => {
          this.total = res.data.total;
          this.results = res.data.results;
        },
        (error) => {
          this.$message.warning("query mistake：" + error.response.status);
        }
      );
    },
    qualityLevelFormat(row) {
      return row.qualityLevel === 1 ? "合格品" : "次品";
    },
    typeFkFormat(row) {
      /**
       * let cmTypesRoot = {
  typeId: 0,
  typeName: "虚拟根节点",
  typeParentId: -1,
  typeLevel: 1,
    };
      * */
      let r = this.$store.state.commodityTypesFlat
      .find(x=>x.typeId === row.typeFk);
      return r.typeName;
    },
  },
  watch: {
    // sampleC(newValue, oldValue) {
    //
    // }
  },
};
</script>

<!--<style scoped>-->
<style lang="scss" scoped></style>
